package com.fr.report.adhoc;

import com.fr.base.ColumnRow;
import com.fr.base.Inter;
import com.fr.base.StringUtils;
import com.fr.base.core.json.JSONArray;
import com.fr.base.core.json.JSONObject;
import com.fr.data.util.function.DataFunction;
import com.fr.report.CellElement;
import com.fr.report.DefaultCellElement;
import com.fr.report.WorkBook;
import com.fr.report.WorkSheet;
import com.fr.report.cellElement.CellExpandAttr;
import com.fr.report.cellElement.Formula;
import com.fr.report.cellElement.core.DSColumn;
import com.fr.report.cellElement.core.FunctionGrouper;
import com.fr.report.cellElement.core.SummaryGrouper;
import com.fr.report.core.SheetUtils;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class CrossedADHOCReport extends NormalADHOCReport
{
  public static int TYPE = 1;
  private ADHOCColumn[] columnColumns;
  private ADHOCColumn[] rowColumns;
  private CrossedADHOCStyle crossedADHOCStyle;

  public ADHOCColumn[] getColumnColumns()
  {
    return this.columnColumns;
  }

  public void setColumnColumns(ADHOCColumn[] paramArrayOfADHOCColumn)
  {
    this.columnColumns = paramArrayOfADHOCColumn;
  }

  public ADHOCColumn[] getRowColumns()
  {
    return this.rowColumns;
  }

  public void setRowColumns(ADHOCColumn[] paramArrayOfADHOCColumn)
  {
    this.rowColumns = paramArrayOfADHOCColumn;
  }

  public CrossedADHOCStyle getCrossedADHOCStyle()
  {
    return this.crossedADHOCStyle;
  }

  public void setCrossedADHOCStyle(CrossedADHOCStyle paramCrossedADHOCStyle)
  {
    this.crossedADHOCStyle = paramCrossedADHOCStyle;
  }

  public WorkBook createWorkBook()
  {
    WorkBook localWorkBook = new WorkBook();
    localWorkBook.putTableData(getViewName(), getADHOCData());
    WorkSheet localWorkSheet = new WorkSheet();
    List localList = cacuCells();
    Iterator localIterator = localList.iterator();
    while (localIterator.hasNext())
      localWorkSheet.addCellElement((DefaultCellElement)localIterator.next());
    if (StringUtils.isNotBlank(getTitle()))
      localWorkSheet.setRowHeight(0, this.crossedADHOCStyle.getRowTitleHeight());
    int i = 0;
    int j = totalWidth();
    while (i < j)
    {
      localWorkSheet.setColumnWidth(i, this.crossedADHOCStyle.getColumnWidth());
      ++i;
    }
    SheetUtils.calculateDefaultParent(localWorkSheet);
    localWorkBook.addReport(localWorkSheet);
    return localWorkBook;
  }

  public int totalWidth()
  {
    return (this.columnColumns.length + this.rowColumns.length + 1);
  }

  private List cacuCells()
  {
    ArrayList localArrayList = new ArrayList();
    if (generateTitleCell(0, 0) != null)
    {
      localArrayList.add(generateTitleCell(0, 0));
      localArrayList.add(generateSubTitleCell(0, 1));
    }
    localArrayList.addAll(generateColumnCells());
    localArrayList.addAll(generateRowCells());
    localArrayList.add(generateSummaryCell());
    localArrayList.addAll(generateOtherSummaryCells());
    return localArrayList;
  }

  private CellElement generateTitleCell(int paramInt1, int paramInt2)
  {
    if (StringUtils.isBlank(getTitle()))
      return null;
    DefaultCellElement localDefaultCellElement = new DefaultCellElement(paramInt1, paramInt2);
    localDefaultCellElement.setColumnSpan(this.columnColumns.length + this.rowColumns.length + 1);
    localDefaultCellElement.setValue(getTitle());
    localDefaultCellElement.setStyle(this.crossedADHOCStyle.getTitleStyle());
    return localDefaultCellElement;
  }

  private CellElement generateSubTitleCell(int paramInt1, int paramInt2)
  {
    if (StringUtils.isBlank(getSubtitle()))
      return null;
    DefaultCellElement localDefaultCellElement = new DefaultCellElement(paramInt1, paramInt2);
    localDefaultCellElement.setColumnSpan(this.columnColumns.length + this.rowColumns.length + 1);
    localDefaultCellElement.setValue(getTitle());
    localDefaultCellElement.setStyle(this.crossedADHOCStyle.getTitleStyle());
    return localDefaultCellElement;
  }

  private List generateColumnCells()
  {
    int i = this.rowColumns.length;
    int j = titleAppend();
    ArrayList localArrayList = new ArrayList();
    int k = 0;
    int l = this.columnColumns.length;
    while (k < l)
    {
      DefaultCellElement localDefaultCellElement1 = new DefaultCellElement(i, j + k);
      localDefaultCellElement1.setStyle(this.crossedADHOCStyle.getReportCotentStyle());
      CellExpandAttr localCellExpandAttr = new CellExpandAttr();
      localCellExpandAttr.setDirection(1);
      localDefaultCellElement1.setCellExpandAttr(localCellExpandAttr);
      localDefaultCellElement1.setColumnSpan(l - k);
      DSColumn localDSColumn = new DSColumn();
      localDSColumn.setColumnName(this.columnColumns[k].getColumnName());
      localDSColumn.setDSName(getViewName());
      localDSColumn.setOrder(this.columnColumns[k].getSort());
      if (k == 0)
        localDSColumn.setCondition(getCondition());
      FunctionGrouper localFunctionGrouper = new FunctionGrouper();
      localFunctionGrouper.setDivideMode(0);
      localFunctionGrouper.setCustom(false);
      localDSColumn.setGrouper(localFunctionGrouper);
      localDefaultCellElement1.setValue(localDSColumn);
      localArrayList.add(localDefaultCellElement1);
      if (k > 0)
      {
        DefaultCellElement localDefaultCellElement2 = new DefaultCellElement(i + l - k, j + k);
        localDefaultCellElement2.setStyle(this.crossedADHOCStyle.getSummaryStyle());
        localDefaultCellElement2.setValue(Inter.getLocText("Summary"));
        localDefaultCellElement2.setRowSpan(l - k);
        localArrayList.add(localDefaultCellElement2);
      }
      ++k;
    }
    return localArrayList;
  }

  private List generateRowCells()
  {
    int i = 0;
    int j = titleAppend() + this.columnColumns.length;
    ArrayList localArrayList = new ArrayList();
    int k = this.columnColumns.length;
    int l = 0;
    int i1 = this.rowColumns.length;
    while (l < i1)
    {
      DefaultCellElement localDefaultCellElement1 = new DefaultCellElement(i + l, j);
      localDefaultCellElement1.setStyle(this.crossedADHOCStyle.getReportCotentStyle());
      CellExpandAttr localCellExpandAttr = new CellExpandAttr();
      localCellExpandAttr.setDirection(0);
      localDefaultCellElement1.setCellExpandAttr(localCellExpandAttr);
      localDefaultCellElement1.setRowSpan(i1 - l);
      DSColumn localDSColumn = new DSColumn();
      localDSColumn.setColumnName(this.rowColumns[l].getColumnName());
      localDSColumn.setDSName(getViewName());
      localDSColumn.setOrder(this.rowColumns[l].getSort());
      if (l == 0)
        localDSColumn.setCondition(getCondition());
      FunctionGrouper localFunctionGrouper = new FunctionGrouper();
      localFunctionGrouper.setDivideMode(0);
      localFunctionGrouper.setCustom(false);
      localDSColumn.setGrouper(localFunctionGrouper);
      localDefaultCellElement1.setValue(localDSColumn);
      localArrayList.add(localDefaultCellElement1);
      if (l > 0)
      {
        DefaultCellElement localDefaultCellElement2 = new DefaultCellElement(i + l, j + i1 - l);
        localDefaultCellElement2.setStyle(this.crossedADHOCStyle.getSummaryStyle());
        localDefaultCellElement2.setValue(Inter.getLocText("Summary"));
        localDefaultCellElement2.setColumnSpan(i1 - l);
        localArrayList.add(localDefaultCellElement2);
      }
      ++l;
    }
    return localArrayList;
  }

  private CellElement generateSummaryCell()
  {
    DefaultCellElement localDefaultCellElement = new DefaultCellElement(summaryCellPosition()[0], summaryCellPosition()[1]);
    localDefaultCellElement.setStyle(this.crossedADHOCStyle.getReportCotentStyle());
    CellExpandAttr localCellExpandAttr = new CellExpandAttr();
    localCellExpandAttr.setDirection(2);
    localDefaultCellElement.setCellExpandAttr(localCellExpandAttr);
    DSColumn localDSColumn = new DSColumn();
    localDSColumn.setColumnName(getSummaryColumn().getColumnName());
    localDSColumn.setDSName(getViewName());
    SummaryGrouper localSummaryGrouper = new SummaryGrouper();
    localSummaryGrouper.setFunction(getSummaryColumn().getFunction());
    localDSColumn.setGrouper(localSummaryGrouper);
    localDefaultCellElement.setValue(localDSColumn);
    return localDefaultCellElement;
  }

  private List generateOtherSummaryCells()
  {
    ArrayList localArrayList = new ArrayList();
    int i = this.columnColumns.length + this.rowColumns.length;
    DefaultCellElement localDefaultCellElement1 = new DefaultCellElement(i, titleAppend());
    localDefaultCellElement1.setStyle(this.crossedADHOCStyle.getSummaryStyle());
    localDefaultCellElement1.setValue(Inter.getLocText("Summary"));
    localDefaultCellElement1.setRowSpan(this.columnColumns.length);
    localArrayList.add(localDefaultCellElement1);
    DefaultCellElement localDefaultCellElement2 = new DefaultCellElement(0, i + titleAppend());
    localDefaultCellElement2.setStyle(this.crossedADHOCStyle.getSummaryStyle());
    localDefaultCellElement2.setValue(Inter.getLocText("Summary"));
    localDefaultCellElement2.setColumnSpan(this.rowColumns.length);
    localArrayList.add(localDefaultCellElement2);
    for (int j = summaryCellPosition()[0]; j < i + 1; ++j)
    {
      int k = summaryCellPosition()[1];
      int l = i + 1 + titleAppend();
      while (k < l)
      {
        if ((j == summaryCellPosition()[0]) && (k == summaryCellPosition()[1]))
          break label297:
        DefaultCellElement localDefaultCellElement3 = new DefaultCellElement(j, k);
        localDefaultCellElement3.setStyle(this.crossedADHOCStyle.getSummaryStyle());
        localDefaultCellElement3.setValue(new Formula(getSummaryColumn().getFunction().getFormula() + "(" + getSummaryCellPlace() + ")"));
        localArrayList.add(localDefaultCellElement3);
        ++k;
      }
    }
    label297: return localArrayList;
  }

  private String getSummaryCellPlace()
  {
    return ColumnRow.valueOf(summaryCellPosition()[0], summaryCellPosition()[1]).toString();
  }

  private int[] summaryCellPosition()
  {
    return new int[] { this.rowColumns.length, this.columnColumns.length + titleAppend() };
  }

  private int titleAppend()
  {
    int i = 0;
    if (StringUtils.isNotEmpty(getTitle()))
      ++i;
    if (StringUtils.isNotEmpty(getSubtitle()))
      ++i;
    return i;
  }

  public JSONObject createJSON()
    throws Exception
  {
    return null;
  }

  public void parseJSON(JSONObject paramJSONObject)
    throws Exception
  {
    JSONArray localJSONArray;
    int i;
    ADHOCColumn[] arrayOfADHOCColumn;
    int j;
    ADHOCColumn localADHOCColumn;
    super.parseJSON(paramJSONObject);
    if (paramJSONObject.has("columns"))
    {
      localJSONArray = paramJSONObject.getJSONArray("columns");
      i = localJSONArray.length();
      arrayOfADHOCColumn = new ADHOCColumn[i];
      for (j = 0; j < i; ++j)
      {
        localADHOCColumn = new ADHOCColumn();
        localADHOCColumn.parseJSON(localJSONArray.getJSONObject(j));
        arrayOfADHOCColumn[j] = localADHOCColumn;
      }
      setColumnColumns(arrayOfADHOCColumn);
    }
    if (paramJSONObject.has("rows"))
    {
      localJSONArray = paramJSONObject.getJSONArray("rows");
      i = localJSONArray.length();
      arrayOfADHOCColumn = new ADHOCColumn[i];
      for (j = 0; j < i; ++j)
      {
        localADHOCColumn = new ADHOCColumn();
        localADHOCColumn.parseJSON(localJSONArray.getJSONObject(j));
        arrayOfADHOCColumn[j] = localADHOCColumn;
      }
      setRowColumns(arrayOfADHOCColumn);
    }
    if (paramJSONObject.has("crossedstyle"))
      setCrossedADHOCStyle(ADHOCReportStyleUtils.getCrossedStyle(paramJSONObject.getInt("crossedstyle")));
  }
}